package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.mapper.AInformationMapper;
import com.team11.server.pojo.AInformation;
import com.team11.server.pojo.dto.ProjectAttendance;
import com.team11.server.pojo.dto.RespPageBean;
import com.team11.server.pojo.dto.vo.ProjectAttendanceVo;
import com.team11.server.service.IAInformationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pjc
 * @since 2021-07-06
 */
@Service
public class AInformationServiceImpl extends ServiceImpl<AInformationMapper, AInformation> implements IAInformationService {

    @Resource
    private AInformationMapper aInformationMapper;

    /**
     * 获取项目考勤记录表
     *
     * @param currentPage 当前页
     * @param size 条数
     * @param projectAttendanceVo 类
     * @return RespPageBean
     */
    @Override
    public RespPageBean getProjectAttendance(Integer currentPage, Integer size, ProjectAttendanceVo projectAttendanceVo) {
        IPage<ProjectAttendance> page = new Page<>(currentPage, size);
        IPage<ProjectAttendance> projectAttendance1 = aInformationMapper.getProjectAttendance(page, projectAttendanceVo);
        return new RespPageBean(projectAttendance1.getTotal(), projectAttendance1.getRecords());
    }

    /**
     * 获取学生考勤列表
     *
     * @param currentPage 当前页
     * @param size 条数
     * @param sNumber 学号
     * @param kcName 课程名称
     * @param aState 考勤状态
     * @return RespPageBean
     */
    @Override
    public RespPageBean getStudentAttendance(Integer currentPage, Integer size, String sNumber,
                                             String kcName, String aState, LocalDate startTime, LocalDate endTime) {
        IPage<ProjectAttendance> page = new Page<>(currentPage, size);
        IPage<ProjectAttendance> studentAttendance = aInformationMapper.getStudentAttendance(page, sNumber,
                kcName, aState, startTime, endTime);
        RespPageBean respPageBean = new RespPageBean(studentAttendance.getTotal(), studentAttendance.getRecords());
        studentAttendance.getRecords().forEach(str -> {
            LocalTime kcIntime = str.getKcInTime();
            LocalTime kcOutTime = str.getKcOutTime();
            LocalTime aRealIn = str.getARealIn();
            LocalTime aRealOut = str.getARealOut();
            Integer lId = str.getLId();
            try {
                System.out.println(kcIntime.until(aRealIn, ChronoUnit.SECONDS));

                if (aRealIn.until(kcIntime, ChronoUnit.SECONDS) >= 0 && aRealIn.until(kcIntime, ChronoUnit.SECONDS)
                        <= 600 && kcOutTime.until(aRealOut, ChronoUnit.SECONDS)
                        >= 0 && kcOutTime.until(aRealOut, ChronoUnit.SECONDS) <= 600) {
                    str.setAState(0);//正常

                } else if ((aRealIn.until(kcIntime, ChronoUnit.SECONDS) < 0 ||
                        aRealIn.until(kcIntime, ChronoUnit.SECONDS) > 600) &&
                        kcOutTime.until(aRealOut, ChronoUnit.SECONDS) >= 0 &&
                        kcOutTime.until(aRealOut, ChronoUnit.SECONDS) <= 600) {
                    str.setAState(1);//迟到

                } else if (aRealIn.until(kcIntime, ChronoUnit.SECONDS) >= 0 &&
                        aRealIn.until(kcIntime, ChronoUnit.SECONDS) <= 600 &&
                        (kcOutTime.until(aRealOut, ChronoUnit.SECONDS) < 0 ||
                                kcOutTime.until(aRealOut, ChronoUnit.SECONDS) > 600)) {
                    str.setAState(2);//早退

                } else if ((aRealIn.until(kcIntime, ChronoUnit.SECONDS) < 0 || aRealIn.until(kcIntime, ChronoUnit.SECONDS) > 600) && (kcOutTime.until(aRealOut, ChronoUnit.SECONDS) < 0 || kcOutTime.until(aRealOut, ChronoUnit.SECONDS) > 600)) {
                    str.setAState(3);//缺勤
                } else if (lId != null) {
                    str.setAState(4);
                }
            } catch (NullPointerException e) {
                System.out.println("错误为: " + e);
            }
            aInformationMapper.updateStudentAttendance(str.getAState(), str.getAId());
            System.out.println(str.getAId() + "-----------");

        });
        return respPageBean;

    }
}
